1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| import os import time import subprocess
def get_time_stamp(t): h = int(t[0:2]) m = int(t[3:5]) s = int(t[6:8]) miu_s = int(t[9:]) return ((h * 60 + m) * 60 + s) * 1000000 + miu_s
last_result = {} cur_result = {} while True: print('---------')
res = subprocess.run('nettop -x -t wifi -t wired -k rx_dupe,rx_ooo,re-tx,rtt_avg,rcvsize,tx_win,tc_class,tc_mgt,cc_algo,P,C,R,W -P -l 1'.split(), stdout=subprocess.PIPE) output = res.stdout.decode() cur_result = {} for line in output.split('\n'): if line == '' or line.startswith('time'): continue data = line.split() data_len = len(data)
t, p, i, o = data[0], ''.join(data[1: data_len - 2]), int(data[data_len - 2]), int(data[data_len - 1]) idx = p.rfind('.') p = p[:idx] t = get_time_stamp(t) cur_result[p] = (t, i, o) if len(last_result) > 0: speed = {} for last_p, last_d in last_result.items(): for cur_p, cur_d in cur_result.items(): if last_p == cur_p: up_speed = cur_d[2] - last_d[2] down_speed = cur_d[1] - last_d[1] if cur_p in speed: speed[cur_p] = (speed[cur_p][0], speed[cur_p][1] + up_speed, speed[cur_p][2] + down_speed) else: speed[cur_p] = (cur_p, up_speed, down_speed) speed = list(speed.values()) speed.sort(key=lambda x: x[1] + x[2], reverse=True) for i in speed: print(i[0]) last_result = cur_result time.sleep(1)
|